package com.have.blog.web.controller;

import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.web.service.LikeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
@RequestMapping("/web/like")
@Api(value = "点赞相关接口", tags = {"点赞相关接口"})
@Slf4j
public class LikeController {
    @Autowired
    private LikeService likeService;

    /**
     * 将用户的点赞行为保存到缓存
     * 定时刷到数据库
     * 问题已经保存到数据库的点赞信息如何更改？如何确定数据库行为是update 还是insert
     * @param uid
     * @return
     */
    @ApiOperation(value = "通过Uid给博客点赞", notes = "通过Uid给博客点赞")
    @PostMapping("/praiseBlogByUid")
    public Wrapper praiseBlogByUid(@ApiParam(name = "uid", value = "博客UID", required = true) @RequestParam(name = "uid", required = true) Integer uid) {
        return likeService.likeOrCancelByBlogUid(uid);
    }

    @ApiOperation(value = "获得博客点赞数", notes = "获得博客点赞数")
    @PostMapping("/getBlogPraiseCount")
    public Wrapper getBlogLikeCount(@ApiParam(name = "uid", value = "博客UID", required = true) @RequestParam(name = "uid", required = true) Integer uid) {
        return Wrapper.success(likeService.getLikeCountByBlogUid(uid));
    }
}
